home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / machine / segar.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  9KB  |  427 lines

  1. /***************************************************************************
  2.  
  3.   machine.c
  4.  
  5.   Functions to emulate general aspects of the machine (RAM, ROM, interrupts,
  6.   I/O ports)
  7.  
  8. ***************************************************************************/
  9.  
  10. #include "driver.h"
  11. #include "vidhrdw/generic.h"
  12.  
  13. void (*sega_decrypt)(int,unsigned int *);
  14.  
  15. unsigned char *segar_mem;
  16.  
  17. WRITE_HANDLER( segar_characterram_w );
  18. WRITE_HANDLER( segar_characterram2_w );
  19. WRITE_HANDLER( segar_colortable_w );
  20. WRITE_HANDLER( segar_bcolortable_w );
  21.  
  22.  
  23. WRITE_HANDLER( segar_w )
  24. {
  25.     int pc,op,page,off;
  26.     unsigned int bad;
  27.  
  28.     off=offset;
  29.  
  30.     pc=cpu_getpreviouspc();
  31.     if (pc != -1)
  32.     {
  33.         op=segar_mem[pc] & 0xFF;
  34.  
  35.         if (op==0x32)
  36.         {
  37.             bad  = offset & 0x00FF;
  38.             page = offset & 0xFF00;
  39.             (*sega_decrypt)(pc,&bad);
  40.             off=page | bad;
  41.         }
  42.     }
  43.  
  44.  
  45.     /* MWA_ROM */
  46.     if      ((off>=0x0000) && (off<=0xC7FF))
  47.     {
  48.         ;
  49.     }
  50.     /* MWA_RAM */
  51.     else if ((off>=0xC800) && (off<=0xCFFF))
  52.     {
  53.         segar_mem[off]=data;
  54.     }
  55.     else if ((off>=0xE000) && (off<=0xE3FF))
  56.     {
  57.         videoram_w(off - 0xE000,data);
  58.     }
  59.     /* MWA_RAM */
  60.     else if ((off>=0xE400) && (off<=0xE7FF))
  61.     {
  62.         segar_mem[off]=data;
  63.     }
  64.     else if ((off>=0xE800) && (off<=0xEFFF))
  65.     {
  66.         segar_characterram_w(off - 0xE800,data);
  67.     }
  68.     else if ((off>=0xF000) && (off<=0xF03F))
  69.     {
  70.         segar_colortable_w(off - 0xF000,data);
  71.     }
  72.     else if ((off>=0xF040) && (off<=0xF07F))
  73.     {
  74.         segar_bcolortable_w(off - 0xF040,data);
  75.     }
  76.     /* MWA_RAM */
  77.     else if ((off>=0xF080) && (off<=0xF7FF))
  78.     {
  79.         segar_mem[off]=data;
  80.     }
  81.     else if ((off>=0xF800) && (off<=0xFFFF))
  82.     {
  83.         segar_characterram2_w(off - 0xF800,data);
  84.     }
  85.     else
  86.     {
  87.         logerror("unmapped write at %04X:%02X\n",off,data);
  88.     }
  89. }
  90.  
  91.  
  92. /****************************************************************************/
  93. /* MB 971025 - Emulate Sega G80 security chip 315-0062                      */
  94. /****************************************************************************/
  95. static void sega_decrypt62(int pc,unsigned int *lo)
  96. {
  97.     unsigned int i = 0;
  98.     unsigned int b = *lo;
  99.  
  100.     switch (pc & 0x03)
  101.     {
  102.         case 0x00:
  103.             /* D */
  104.             i=b & 0x23;
  105.             i+=((b    & 0xC0) >> 4);
  106.             i+=((b    & 0x10) << 2);
  107.             i+=((b    & 0x08) << 1);
  108.             i+=(((~b) & 0x04) << 5);
  109.             i &= 0xFF;
  110.             break;
  111.         case 0x01:
  112.             /* C */
  113.             i=b & 0x03;
  114.             i+=((b    & 0x80) >> 4);
  115.             i+=(((~b) & 0x40) >> 1);
  116.             i+=((b    & 0x20) >> 1);
  117.             i+=((b    & 0x10) >> 2);
  118.             i+=((b    & 0x08) << 3);
  119.             i+=((b    & 0x04) << 5);
  120.             i &= 0xFF;
  121.             break;
  122.         case 0x02:
  123.             /* B */
  124.             i=b & 0x03;
  125.             i+=((b    & 0x80) >> 1);
  126.             i+=((b    & 0x60) >> 3);
  127.             i+=((~b) & 0x10);
  128.             i+=((b    & 0x08) << 2);
  129.             i+=((b    & 0x04) << 5);
  130.             i &= 0xFF;
  131.             break;
  132.         case 0x03:
  133.             /* A */
  134.             i=b;
  135.             break;
  136.     }
  137.  
  138.     *lo=i;
  139. }
  140.  
  141. /****************************************************************************/
  142. /* MB 971025 - Emulate Sega G80 security chip 315-0063                      */
  143. /****************************************************************************/
  144. static void sega_decrypt63(int pc,unsigned int *lo)
  145. {
  146.     unsigned int i = 0;
  147.     unsigned int b = *lo;
  148.  
  149.     switch (pc & 0x09)
  150.     {
  151.         case 0x00:
  152.             /* D */
  153.             i=b & 0x23;
  154.             i+=((b    & 0xC0) >> 4);
  155.             i+=((b    & 0x10) << 2);
  156.             i+=((b    & 0x08) << 1);
  157.             i+=(((~b) & 0x04) << 5);
  158.             i &= 0xFF;
  159.             break;
  160.         case 0x01:
  161.             /* C */
  162.             i=b & 0x03;
  163.             i+=((b    & 0x80) >> 4);
  164.             i+=(((~b) & 0x40) >> 1);
  165.             i+=((b    & 0x20) >> 1);
  166.             i+=((b    & 0x10) >> 2);
  167.             i+=((b    & 0x08) << 3);
  168.             i+=((b    & 0x04) << 5);
  169.             i &= 0xFF;
  170.             break;
  171.         case 0x08:
  172.             /* B */
  173.             i=b & 0x03;
  174.             i+=((b    & 0x80) >> 1);
  175.             i+=((b    & 0x60) >> 3);
  176.             i+=((~b) & 0x10);
  177.             i+=((b    & 0x08) << 2);
  178.             i+=((b    & 0x04) << 5);
  179.             i &= 0xFF;
  180.             break;
  181.         case 0x09:
  182.             /* A */
  183.             i=b;
  184.             break;
  185.     }
  186.  
  187.     *lo=i;
  188. }
  189.  
  190. /****************************************************************************/
  191. /* MB 971025 - Emulate Sega G80 security chip 315-0064                      */
  192. /****************************************************************************/
  193. static void sega_decrypt64(int pc,unsigned int *lo)
  194. {
  195.     unsigned int i = 0;
  196.     unsigned int b = *lo;
  197.  
  198.     switch (pc & 0x03)
  199.     {
  200.         case 0x00:
  201.             /* A */
  202.             i=b;
  203.             break;
  204.         case 0x01:
  205.             /* B */
  206.             i=b & 0x03;
  207.             i+=((b    & 0x80) >> 1);
  208.             i+=((b    & 0x60) >> 3);
  209.             i+=((~b) & 0x10);
  210.             i+=((b    & 0x08) << 2);
  211.             i+=((b    & 0x04) << 5);
  212.             i &= 0xFF;
  213.             break;
  214.         case 0x02:
  215.             /* C */
  216.             i=b & 0x03;
  217.             i+=((b    & 0x80) >> 4);
  218.             i+=(((~b) & 0x40) >> 1);
  219.             i+=((b    & 0x20) >> 1);
  220.             i+=((b    & 0x10) >> 2);
  221.             i+=((b    & 0x08) << 3);
  222.             i+=((b    & 0x04) << 5);
  223.             i &= 0xFF;
  224.             break;
  225.         case 0x03:
  226.             /* D */
  227.             i=b & 0x23;
  228.             i+=((b    & 0xC0) >> 4);
  229.             i+=((b    & 0x10) << 2);
  230.             i+=((b    & 0x08) << 1);
  231.             i+=(((~b) & 0x04) << 5);
  232.             i &= 0xFF;
  233.             break;
  234.     }
  235.  
  236.     *lo=i;
  237. }
  238.  
  239.  
  240. /****************************************************************************/
  241. /* MB 971025 - Emulate Sega G80 security chip 315-0070                      */
  242. /****************************************************************************/
  243. static void sega_decrypt70(int pc,unsigned int *lo)
  244. {
  245.     unsigned int i = 0;
  246.     unsigned int b = *lo;
  247.  
  248.     switch (pc & 0x09)
  249.     {
  250.         case 0x00:
  251.             /* B */
  252.             i=b & 0x03;
  253.             i+=((b    & 0x80) >> 1);
  254.             i+=((b    & 0x60) >> 3);
  255.             i+=((~b) & 0x10);
  256.             i+=((b    & 0x08) << 2);
  257.             i+=((b    & 0x04) << 5);
  258.             i &= 0xFF;
  259.             break;
  260.         case 0x01:
  261.             /* A */
  262.             i=b;
  263.             break;
  264.         case 0x08:
  265.             /* D */
  266.             i=b & 0x23;
  267.             i+=((b    & 0xC0) >> 4);
  268.             i+=((b    & 0x10) << 2);
  269.             i+=((b    & 0x08) << 1);
  270.             i+=(((~b) & 0x04) << 5);
  271.             i &= 0xFF;
  272.             break;
  273.         case 0x09:
  274.             /* C */
  275.             i=b & 0x03;
  276.             i+=((b    & 0x80) >> 4);
  277.             i+=(((~b) & 0x40) >> 1);
  278.             i+=((b    & 0x20) >> 1);
  279.             i+=((b    & 0x10) >> 2);
  280.             i+=((b    & 0x08) << 3);
  281.             i+=((b    & 0x04) << 5);
  282.             i &= 0xFF;
  283.             break;
  284.     }
  285.  
  286.     *lo=i;
  287. }
  288.  
  289. /****************************************************************************/
  290. /* MB 971025 - Emulate Sega G80 security chip 315-0076                      */
  291. /****************************************************************************/
  292. static void sega_decrypt76(int pc,unsigned int *lo)
  293. {
  294.     unsigned int i = 0;
  295.     unsigned int b = *lo;
  296.  
  297.     switch (pc & 0x09)
  298.     {
  299.         case 0x00:
  300.             /* A */
  301.             i=b;
  302.             break;
  303.         case 0x01:
  304.             /* B */
  305.             i=b & 0x03;
  306.             i+=((b    & 0x80) >> 1);
  307.             i+=((b    & 0x60) >> 3);
  308.             i+=((~b) & 0x10);
  309.             i+=((b    & 0x08) << 2);
  310.             i+=((b    & 0x04) << 5);
  311.             i &= 0xFF;
  312.             break;
  313.         case 0x08:
  314.             /* C */
  315.             i=b & 0x03;
  316.             i+=((b    & 0x80) >> 4);
  317.             i+=(((~b) & 0x40) >> 1);
  318.             i+=((b    & 0x20) >> 1);
  319.             i+=((b    & 0x10) >> 2);
  320.             i+=((b    & 0x08) << 3);
  321.             i+=((b    & 0x04) << 5);
  322.             i &= 0xFF;
  323.             break;
  324.         case 0x09:
  325.             /* D */
  326.             i=b & 0x23;
  327.             i+=((b    & 0xC0) >> 4);
  328.             i+=((b    & 0x10) << 2);
  329.             i+=((b    & 0x08) << 1);
  330.             i+=(((~b) & 0x04) << 5);
  331.             i &= 0xFF;
  332.             break;
  333.     }
  334.  
  335.     *lo=i;
  336. }
  337.  
  338. /****************************************************************************/
  339. /* MB 971025 - Emulate Sega G80 security chip 315-0082                      */
  340. /****************************************************************************/
  341. static void sega_decrypt82(int pc,unsigned int *lo)
  342. {
  343.     unsigned int i = 0;
  344.     unsigned int b = *lo;
  345.  
  346.     switch (pc & 0x11)
  347.     {
  348.         case 0x00:
  349.             /* A */
  350.             i=b;
  351.             break;
  352.         case 0x01:
  353.             /* B */
  354.             i=b & 0x03;
  355.             i+=((b    & 0x80) >> 1);
  356.             i+=((b    & 0x60) >> 3);
  357.             i+=((~b) & 0x10);
  358.             i+=((b    & 0x08) << 2);
  359.             i+=((b    & 0x04) << 5);
  360.             i &= 0xFF;
  361.             break;
  362.         case 0x10:
  363.             /* C */
  364.             i=b & 0x03;
  365.             i+=((b    & 0x80) >> 4);
  366.             i+=(((~b) & 0x40) >> 1);
  367.             i+=((b    & 0x20) >> 1);
  368.             i+=((b    & 0x10) >> 2);
  369.             i+=((b    & 0x08) << 3);
  370.             i+=((b    & 0x04) << 5);
  371.             i &= 0xFF;
  372.             break;
  373.         case 0x11:
  374.             /* D */
  375.             i=b & 0x23;
  376.             i+=((b    & 0xC0) >> 4);
  377.             i+=((b    & 0x10) << 2);
  378.             i+=((b    & 0x08) << 1);
  379.             i+=(((~b) & 0x04) << 5);
  380.             i &= 0xFF;
  381.             break;
  382.     }
  383.  
  384.     *lo=i;
  385. }
  386.  
  387. /****************************************************************************/
  388. /* MB 971031 - Emulate no Sega G80 security chip                            */
  389. /****************************************************************************/
  390. static void sega_decrypt0(int pc,unsigned int *lo)
  391. {
  392.         return;
  393. }
  394.  
  395. /****************************************************************************/
  396. /* MB 971025 - Set the security chip to be used                             */
  397. /****************************************************************************/
  398. void sega_security(int chip)
  399. {
  400.     switch (chip)
  401.     {
  402.         case 62:
  403.             sega_decrypt=sega_decrypt62;
  404.             break;
  405.         case 63:
  406.             sega_decrypt=sega_decrypt63;
  407.             break;
  408.         case 64:
  409.             sega_decrypt=sega_decrypt64;
  410.             break;
  411.         case 70:
  412.             sega_decrypt=sega_decrypt70;
  413.             break;
  414.         case 76:
  415.             sega_decrypt=sega_decrypt76;
  416.             break;
  417.         case 82:
  418.             sega_decrypt=sega_decrypt82;
  419.             break;
  420.         default:
  421.             sega_decrypt=sega_decrypt0;
  422.             break;
  423.     }
  424. }
  425.  
  426.  
  427.